using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace net.notdot.Attercop.Client
{
    public class CommandParser
    {
        public enum CommandType
        {
            Invalid = 0,
            ChatMessage = 1,
            Emote = 2
        }

        public struct Command
        {
            public CommandType Type;
            public string[] Parameters;
        }

        private static Regex isCommand = new Regex("^/[^/]");
        private static Regex[] commandRegex = new Regex[] {
            null,                                                       //Invalid
            null,                                                       //ChatMessage
            new Regex("^/(?:me|em)\\s+(.*)$")                           //Emote
        };

        public static Command Parse(string s)
        {
            Command ret = new Command();

            if(!isCommand.IsMatch(s)) {
                ret.Type = CommandType.ChatMessage;
                ret.Parameters = new string[] { (s.Length > 1 && s[0] == s[1] && s[1] == '/')?s.Substring(1):s };
                return ret;
            }

            for(int i = 0; i < commandRegex.Length; i++) {
                if (commandRegex[i] == null)
                    continue;

                Match m = commandRegex[i].Match(s);
                if(m.Success) {
                    ret.Type = (CommandType)i;
                    ret.Parameters = new string[m.Groups.Count - 1];
                    for(int j = 1; j < m.Groups.Count; j++)
                        ret.Parameters[j - 1] = m.Groups[j].Value;

                    return ret;
                }
            }

            return ret;
        }
    }
}
